//
//  AutoReply.m
//  reSearchDemo
//
//  Created by 科技 on 15/8/3.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "AutoReply.h"
#import "AutoReplyView1.h"
#import "AutoReplyView2.h"
#import "AutoReplyView3.h"
#import "AutoReplyView4.h"
#import "UIView+Layer.h"
#import "CameraActionSheet.h"
#import "VPImageCropperViewController.h"
#import "KAlertView.h"
#define CGRectHeadViewHeight  44
typedef NS_ENUM(NSInteger,XCYMenu) {
    oneMenu,
    twoMenu,
    threeMenu
};
@interface AutoReply ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate,CameraActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,UITextFieldDelegate>
{
    NSMutableArray *BtnArr;            /**< 存放menu按钮的数组*/
    UIView *lineView;                  /**< 随菜单滚动的下划线*/
    NSMutableArray *secondaryBtnArr;   /**< 存放二级menu按钮的数组*/
    UIView *secondaryLineView;         /**< 随二级菜单滚动的下划线*/
    
    UIScrollView *_ScrollView;         /**< 存放菜单视图的ScrollView*/
    UIView *_headView;                 /**< tableview的头视图 */
    UITableView *_tableView;           /**< tableView       */
    
    int offset;                     /**< 当前的偏移量     */
    CGFloat _oldOffsetY;             /**< scrollView的偏移量 */
    CGFloat _oldOffsetX;
    UIButton *SelectBtn;             /**< 上传图片的选中按钮 */
    AutoReplyView1 *view1;           /**< (弃用)文字发送视图      */
    AutoReplyView2 *view2;           /**< 图片发送视图      */
    AutoReplyView3 *view3;           /**< 单条新闻发送视图   */
    AutoReplyView4 *view4;           /**< 文字发送视图 */
}
/**
 *  一级菜单选择的样式
 */
@property(nonatomic,assign)XCYMenu MenuType;

@end

@implementation AutoReply

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"自动回复";
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_tableView.frame), CGRectGetHeight(_tableView.frame))];
    
    [self AllocMenu];
    [self AllocScrollView];
    [self AllocSecondaryMenu];
    
    [_tableView setTableHeaderView:_headView];
    _MenuType = 0;
}
#pragma mark --初始化scrollView--
-(void)AllocScrollView {
    
    _ScrollView = [[UIScrollView alloc] init];
    _ScrollView.frame = CGRectMake(0, CGRectHeadViewHeight*2, CGRectGetWidth(self.view.frame), 1000 + 50);
    _ScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame)*4, 0);
    _ScrollView.showsHorizontalScrollIndicator = NO;
    _ScrollView.showsVerticalScrollIndicator = NO;
    _ScrollView.pagingEnabled=YES;
    _ScrollView.delegate = self;
    //    _ScrollView.contentInset = UIEdgeInsetsMake(20, 0, 20, 0);

    [_headView addSubview:_ScrollView];
    [self SetTextView];
    [self setImgView];
    [self SetNewsView];
    [self SetMoreNewsView];
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"付款" forKey:@"ext"];
    [dic setObject:[NSString stringWithFormat:@"%d",1] forKey:@"paytype"];
    [dic setObject:@"12345" forKey:@"much"];
    UIImage *image = [UIImage imageNamed:@"count"];
    [[HTTPGlobals shareHTTPGlobal] uploadImageWithUrl:@"MobilePort/agent/add_remitlog.html" image:image imageName:@"img" parameters:dic finish:^(BOOL seccess, NSError *error, id obj) {
        if (seccess) {
            DLog(@"obj = %@",obj);
            DLog(@"msg = %@",obj[@"msg"]);
        }else {
            DLog(@"error = %@",error);
        }
    }];
    
    //自动回复设置列表
    [[HTTPGlobals shareHTTPGlobal] GetHTTPRequest:@"MobilePort/Index/Autoreply/msgAutoreplyList" parameters:nil finish:^(BOOL seccess, NSString *error, id obj) {
        if (seccess) {
            DLog(@"obj = %@",obj);
            DLog(@"obj.info = %@",obj[@"info"]);
        }
    }];
    //获取自动回复开启状态
    [[HTTPGlobals shareHTTPGlobal] GetHTTPRequest:@"MobilePort/Index/Autoreply/getAutoreplyOpen" parameters:nil finish:^(BOOL seccess, NSString *error, id obj) {
        if (seccess) {
            DLog(@"obj = %@",obj);
            DLog(@"obj.info = %@",obj[@"info"]);
        }
    }];
}
#pragma mark --初始化菜单
-(void)AllocMenu {
    BtnArr = [NSMutableArray array];
    NSArray *titleArr = @[@"被添加自动回复",@"消息自动回复",@"关键词自动回复"];
    for (int i = 0; i<titleArr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(CGRectGetWidth(self.view.frame)/titleArr.count*i, 0, CGRectGetWidth(self.view.frame)/titleArr.count, 44);
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setBackgroundColor:[UIColor clearColor]];
        [btn setTitle:titleArr[i] forState:0];
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(MenuBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            btn.selected = YES;
        }
        [BtnArr addObject:btn];
        [_headView addSubview:btn];
        
    }
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectHeadViewHeight, CGRectGetWidth(self.view.frame), 2)];
    line.backgroundColor = [UIColor lightGrayColor];
    lineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectHeadViewHeight, CGRectGetWidth(self.view.frame)/titleArr.count, 2)];
    lineView.backgroundColor = [UIColor orangeColor];
    [_headView addSubview:line];
    [_headView addSubview:lineView];
    
}
#pragma mark --初始化二级菜单--
-(void)AllocSecondaryMenu {
    secondaryBtnArr = [NSMutableArray array];
    NSArray *titleArr = @[@"文字",@"图片",@"单条新闻",@"多条新闻"];
    for (int i = 0; i<titleArr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(CGRectGetWidth(self.view.frame)/titleArr.count*i + 10, CGRectHeadViewHeight, (CGRectGetWidth(self.view.frame)-60)/titleArr.count, 44);
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setBackgroundColor:[UIColor clearColor]];
        [btn setTitle:titleArr[i] forState:0];
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(SecondaryMenuBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            btn.selected = YES;
        }
        [secondaryBtnArr addObject:btn];
        [_headView addSubview:btn];
        
    }
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectHeadViewHeight*2, CGRectGetWidth(self.view.frame) - 20, 2)];
    line.backgroundColor = [UIColor lightGrayColor];
    secondaryLineView = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectHeadViewHeight*2, (CGRectGetWidth(self.view.frame) - 60)/titleArr.count, 2)];
    secondaryLineView.backgroundColor = [UIColor orangeColor];
    [_headView addSubview:line];
    [_headView addSubview:secondaryLineView];
}
#pragma mark --二级菜单按钮响应函数--
-(void)SecondaryMenuBtnAction:(UIButton *)sender {
    int index = 0;
    for (int i = 0; i<secondaryBtnArr.count; i++) {
        UIButton *btn = secondaryBtnArr[i];
        btn.selected = NO;
        if (sender == secondaryBtnArr[i]) {
            index = i;
        }
    }
    [UIView animateWithDuration:0.2 animations:^{
        _ScrollView.contentOffset = CGPointMake(index * _ScrollView.frame.size.width, 0);
        sender.selected = YES;
        secondaryLineView.frame = CGRectMake(CGRectGetMinX(sender.frame), CGRectHeadViewHeight*2,CGRectGetWidth(sender.frame), 2);
    } completion:^(BOOL finished) {
        
    }];
}
#pragma mark --一级菜单按钮响应函数--
-(void)MenuBtnAction:(UIButton *)sender {
    int index = 0;
    for (int i = 0; i<BtnArr.count; i++) {
        UIButton *btn = BtnArr[i];
        btn.selected = NO;
        if (sender == BtnArr[i]) {
            index = i;
        }
    }
    [UIView animateWithDuration:0.2 animations:^{
        _ScrollView.contentOffset = CGPointMake( 0, 0);
        for (int i = 0; i<secondaryBtnArr.count; i++) {
            UIButton *btn = secondaryBtnArr[i];
            if (i == 0) {
                btn.selected = YES;
            }else {
                btn.selected = NO;
            }
        }
        secondaryLineView.frame = CGRectMake(10, CGRectHeadViewHeight*2, (CGRectGetWidth(self.view.frame) - 60)/secondaryBtnArr.count, 2);
        _MenuType = index;
        sender.selected = YES;
        lineView.frame = CGRectMake(CGRectGetWidth(self.view.frame)/BtnArr.count*index, CGRectHeadViewHeight, CGRectGetWidth(self.view.frame)/BtnArr.count, 2);
    } completion:^(BOOL finished) {
        
    }];
    
}
#pragma mark-
#pragma mark --加载文字页面 --
-(void)SetTextView {
    
    view4 = [AutoReplyView4 instanceView];
    view4.frame = CGRectMake(0, 10, CGRectGetWidth(_ScrollView.frame), 330);
    view4.backgroundColor = [UIColor lightGrayColor];
    view4.TF_1.delegate = self;
    view4.TF_2.delegate = self;
    view4.TF_3.delegate = self;
    [view4.sureBtn addTarget:self action:@selector(TextBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_ScrollView addSubview:view4];
}
#pragma mark --加载图片页面--
-(void)setImgView {
    view2 = [AutoReplyView2 instanceView];
    view2.frame = CGRectMake(_ScrollView.frame.size.width, 10, CGRectGetWidth(_ScrollView.frame), 410);
    view2.TF_1.delegate = self;
    view2.TF_2.delegate = self;
    view2.TF_3.delegate = self;
    [view2.ImageBtn addTarget:self action:@selector(ImageViewBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [view2.sureBtn addTarget:self action:@selector(ImageBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    view2.backgroundColor = [UIColor lightGrayColor];
    [_ScrollView addSubview:view2];
}
#pragma mark --加载单条新闻页面--
-(void)SetNewsView {
    view3 = [AutoReplyView3 instanceView];
    view3.frame = CGRectMake(_ScrollView.frame.size.width*2, 10, CGRectGetWidth(_ScrollView.frame), 500);
    view3.textTitle.delegate = self;
    view3.textIntroduction.delegate = self;
    view3.textDetails.delegate = self;
    view3.TF_1.delegate = self;
    view3.TF_2.delegate = self;
    [view3.ImageBtn addTarget:self action:@selector(ImageViewBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [view3.sureBtn addTarget:self action:@selector(OneNewsBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    view3.backgroundColor = [UIColor lightGrayColor];
    [_ScrollView addSubview:view3];
}
#pragma mark --加载多条新闻页面--
-(void)SetMoreNewsView {
    UIView *BGview = [[UIView alloc] initWithFrame:CGRectMake(_ScrollView.frame.size.width*3, 10, CGRectGetWidth(_ScrollView.frame), 100)];
    BGview.backgroundColor = [UIColor lightGrayColor];
    UILabel *ruleName = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 200, 20)];
    ruleName.text = @"规则名:";
    UITextField *ruleNameTF = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(ruleName.frame), CGRectGetMaxY(ruleName.frame) + 5, CGRectGetWidth(_ScrollView.frame) - CGRectGetMinX(ruleName.frame)*2, 30)];
//    ruleNameTF.placeholder = @"文字输入";
    ruleNameTF.borderStyle = UITextBorderStyleRoundedRect;
    ruleNameTF.tag = 3000;
    ruleNameTF.delegate = self;
    UILabel *promptLable = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(ruleNameTF.frame), CGRectGetMaxY(ruleNameTF.frame) + 10, CGRectGetWidth(_ScrollView.frame), 20)];
    promptLable.text = @"（一次最多3个关键词,中间用空格分隔）";
    promptLable.font = [UIFont systemFontOfSize:17];
    UIView *promptLane = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(promptLable.frame), CGRectGetMaxY(promptLable.frame), CGRectGetWidth(self.view.frame) - CGRectGetMinX(promptLable.frame)*2, 1)];
    promptLane.backgroundColor = [UIColor darkGrayColor];
    
    UILabel *keyword = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(promptLable.frame), CGRectGetMaxY(promptLable.frame) + 10, 200, 20)];
    keyword.text = @"关键词:";
    UITextField *keywordTF = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(keyword.frame), CGRectGetMaxY(keyword.frame) + 5, CGRectGetWidth(_ScrollView.frame) - CGRectGetMinX(keyword.frame)*2, 30)];
//    keywordTF.placeholder = @"文字输入";
    keywordTF.borderStyle = UITextBorderStyleRoundedRect;
    keywordTF.tag = 3001;
    keywordTF.delegate = self;
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(keywordTF.frame),CGRectGetMaxY(keywordTF.frame) + 10, 200, 20)];
    label1.text = @"新闻标题:";
    UITextField *textfield1 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(label1.frame), CGRectGetMaxY(label1.frame) + 5, CGRectGetWidth(_ScrollView.frame) - CGRectGetMinX(label1.frame)*2, 30)];
    textfield1.placeholder = @"文字输入";
    textfield1.borderStyle = UITextBorderStyleRoundedRect;
    textfield1.tag = 3002;
    textfield1.delegate = self;
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(textfield1.frame), CGRectGetMaxY(textfield1.frame) + 10, 200, 20)];
    label2.text = @"新闻缩略图";
    UIButton *imageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    imageBtn.frame = CGRectMake(CGRectGetMinX(label2.frame), CGRectGetMaxY(label2.frame) + 5, CGRectGetWidth(BGview.frame) - CGRectGetMinX(label2.frame)*2, 30);
    [imageBtn setTitle:@"上传文件" forState:UIControlStateNormal];
    imageBtn.backgroundColor = [UIColor colorWithRed:229/255.0 green:122/255.0 blue:122/255.0 alpha:0.9];
    [UIView SetRoundedCorners:imageBtn andcornerRadius:10.0 andborderWidth:1.0 andborderColor:[UIColor clearColor]];
    [imageBtn addTarget:self action:@selector(ImageViewBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    imageBtn.tag = 3010;
    

    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(imageBtn.frame), CGRectGetMaxY(imageBtn.frame) + 10, 200, 20)];
    label3.text = @"新闻详情";
    UITextField *textfield3 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(label3.frame), CGRectGetMaxY(label3.frame) + 5, CGRectGetWidth(_ScrollView.frame) -CGRectGetMinX(label3.frame)*2, 30)];
    textfield3.placeholder = @"文字输入";
    textfield3.borderStyle = UITextBorderStyleRoundedRect;
    textfield3.tag = 3003;
    textfield3.delegate = self;

    UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(textfield3.frame), CGRectGetMaxY(textfield3.frame) + 10, 200, 20)];
    label4.text = @"新闻标题";
    
    UITextField *textfield4 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(label4.frame), CGRectGetMaxY(label4.frame) + 10, CGRectGetWidth(BGview.frame) - CGRectGetMinX(label4.frame)*2, 30)];
    textfield4.placeholder = @"文字输入";
    textfield4.borderStyle = UITextBorderStyleRoundedRect;
    textfield4.tag = 3004;
    textfield4.delegate = self;
    
    UILabel *label5 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(textfield4.frame), CGRectGetMaxY(textfield4.frame) + 10, 200, 20)];
    label5.text = @"新闻缩略图";
    UIButton *imageBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    imageBtn1.frame = CGRectMake(CGRectGetMinX(label5.frame), CGRectGetMaxY(label5.frame) + 5, CGRectGetWidth(BGview.frame) - CGRectGetMinX(label5.frame)*2, 30);
    [imageBtn1 setTitle:@"上传文件" forState:UIControlStateNormal];
    imageBtn1.backgroundColor = [UIColor colorWithRed:229/255.0 green:122/255.0 blue:122/255.0 alpha:0.9];
    [UIView SetRoundedCorners:imageBtn1 andcornerRadius:10.0 andborderWidth:1.0 andborderColor:[UIColor clearColor]];
    [imageBtn1 addTarget:self action:@selector(ImageViewBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    imageBtn1.tag = 3011;
    
    UILabel *label6 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(imageBtn1.frame), CGRectGetMaxY(imageBtn1.frame) + 10, 200, 20)];
    label6.text = @"新闻详情";
    
    UITextField *textfield6 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(label6.frame), CGRectGetMaxY(label6.frame) + 5, CGRectGetWidth(_ScrollView.frame) -CGRectGetMinX(label6.frame)*2, 30)];
    textfield6.placeholder = @"文字输入";
    textfield6.borderStyle = UITextBorderStyleRoundedRect;
    textfield6.tag = 3005;
    textfield6.delegate = self;

    UILabel *label7 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(textfield6.frame), CGRectGetMaxY(textfield6.frame) + 10, CGRectGetWidth(BGview.frame) - CGRectGetMinX(textfield6.frame)*2, 20)];
    label7.text = @"新闻标题:";
    UITextField *textfield7 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(label7.frame), CGRectGetMaxY(label7.frame) + 5, CGRectGetWidth(_ScrollView.frame) - CGRectGetMinX(label7.frame)*2, 30)];
    textfield7.placeholder = @"文字输入";
    textfield7.borderStyle = UITextBorderStyleRoundedRect;
    textfield7.tag = 3006;
    textfield7.delegate = self;
    
    UILabel *label8 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(textfield7.frame), CGRectGetMaxY(textfield7.frame) + 10, 200, 20)];
    label8.text = @"新闻缩略图";
    UIButton *imageBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    imageBtn2.frame = CGRectMake(CGRectGetMinX(label8.frame), CGRectGetMaxY(label8.frame) + 5, CGRectGetWidth(BGview.frame) - CGRectGetMinX(label8.frame)*2, 30);
    [imageBtn2 setTitle:@"上传文件" forState:UIControlStateNormal];
    imageBtn2.backgroundColor = [UIColor colorWithRed:229/255.0 green:122/255.0 blue:122/255.0 alpha:0.9];
    [UIView SetRoundedCorners:imageBtn2 andcornerRadius:10.0 andborderWidth:1.0 andborderColor:[UIColor clearColor]];
    [imageBtn2 addTarget:self action:@selector(ImageViewBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    imageBtn2.tag = 3012;
    
    UILabel *label9 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(imageBtn2.frame), CGRectGetMaxY(imageBtn2.frame) + 10, 200, 20)];
    label9.text = @"新闻详情";
    
    UITextField *textfield9 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(label9.frame), CGRectGetMaxY(label9.frame) + 10, CGRectGetWidth(BGview.frame) - CGRectGetMinX(label9.frame)*2, 30)];
    textfield9.placeholder = @"文字输入";
    textfield9.borderStyle = UITextBorderStyleRoundedRect;
    textfield9.tag = 3007;
    textfield9.delegate = self;
    
    UILabel *label10 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(textfield9.frame), CGRectGetMaxY(textfield9.frame) + 10, CGRectGetWidth(BGview.frame) - CGRectGetMinX(textfield9.frame)*2, 20)];
    label10.text = @"新闻标题:";
    UITextField *textfield10 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(label10.frame), CGRectGetMaxY(label10.frame) + 5, CGRectGetWidth(_ScrollView.frame) - CGRectGetMinX(label10.frame)*2, 30)];
    textfield10.placeholder = @"文字输入";
    textfield10.borderStyle = UITextBorderStyleRoundedRect;
    textfield10.tag = 3008;
    textfield10.delegate = self;
    
    UILabel *label11 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(textfield10.frame), CGRectGetMaxY(textfield10.frame) + 10, 200, 20)];
    label11.text = @"新闻缩略图";
    UIButton *imageBtn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    imageBtn3.frame = CGRectMake(CGRectGetMinX(label11.frame), CGRectGetMaxY(label11.frame) + 5, CGRectGetWidth(BGview.frame) - CGRectGetMinX(label11.frame)*2, 30);
    [imageBtn3 setTitle:@"上传文件" forState:UIControlStateNormal];
    imageBtn3.backgroundColor = [UIColor colorWithRed:229/255.0 green:122/255.0 blue:122/255.0 alpha:0.9];
    [UIView SetRoundedCorners:imageBtn3 andcornerRadius:10.0 andborderWidth:1.0 andborderColor:[UIColor clearColor]];
    [imageBtn3 addTarget:self action:@selector(ImageViewBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    imageBtn3.tag = 3013;
    
    UILabel *label12 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(imageBtn3.frame), CGRectGetMaxY(imageBtn3.frame) + 10, 200, 20)];
    label12.text = @"新闻详情";
    
    UITextField *textfield12 = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMinX(label12.frame), CGRectGetMaxY(label12.frame) + 10, CGRectGetWidth(BGview.frame) - CGRectGetMinX(label12.frame)*2, 30)];
    textfield12.placeholder = @"文字输入";
    textfield12.borderStyle = UITextBorderStyleRoundedRect;
    textfield12.tag = 3009;
    textfield12.delegate = self;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(CGRectGetMinX(textfield12.frame), CGRectGetMaxY(textfield12.frame) + 20, 64, 30);
    [btn setTitle:@"群发" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor whiteColor];
    [UIView SetRoundedCorners:btn andcornerRadius:5.0 andborderWidth:1.0 andborderColor:[UIColor clearColor]];
    [btn setBackgroundColor:[UIColor colorWithRed:55.0/255.0 green:99.0/255.0 blue:142.0/255.0 alpha:0.9]];
    [btn addTarget:self action:@selector(MoreNewsBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    BGview.frame = CGRectMake(_ScrollView.frame.size.width*3, 10, CGRectGetWidth(_ScrollView.frame), CGRectGetMaxY(btn.frame) + CGRectGetHeight(btn.frame) + 100);
    
    [BGview addSubview:ruleName];
    [BGview addSubview:ruleNameTF];
    [BGview addSubview:promptLable];
    [BGview addSubview:promptLane];
    [BGview addSubview:keyword];
    [BGview addSubview:keywordTF];
    [BGview addSubview:label1];
    [BGview addSubview:textfield1];
    [BGview addSubview:label2];
    [BGview addSubview:imageBtn];
    [BGview addSubview:label3];
    [BGview addSubview:textfield3];
    [BGview addSubview:label4];
    [BGview addSubview:textfield4];
    [BGview addSubview:label5];
    [BGview addSubview:imageBtn1];
    [BGview addSubview:label6];
    [BGview addSubview:textfield6];
    [BGview addSubview:label7];
    [BGview addSubview:textfield7];
    [BGview addSubview:label8];
    [BGview addSubview:imageBtn2];
    [BGview addSubview:btn];
    [BGview addSubview:label9];
    [BGview addSubview:textfield9];
    [BGview addSubview:label10];
    [BGview addSubview:textfield10];
    [BGview addSubview:label11];
    [BGview addSubview:imageBtn3];
    [BGview addSubview:label12];
    [BGview addSubview:textfield12];
    [_ScrollView addSubview:BGview];
}
#pragma mark --调用相册，相机按钮--
-(void)ImageViewBtnAction:(UIButton *)sender {
    CameraActionSheet *actionSheet = [[CameraActionSheet alloc] initWithActionTitle:nil TextViews:nil CancelTitle:@"取消" withDelegate:self otherButtonTitles:@"从相册选择", @"拍一张", nil];
    [actionSheet show];
    SelectBtn = sender;
}
#pragma mark -
#pragma mark --文字发送按钮--
-(void)TextBtnAction:(UIButton *)sender {
    [self showText:@"文字发送按钮"];
    if (view4.TF_1.text.length <= 0 || view4.TF_2.text.length <= 0 || view4.TF_3.text.length <= 0) {
        UIAlertView *alert = [self showAlertViewTitle:@"提示:" message:@"内容不能为空" Btntitle:@"确定"];
        [alert show];
        return;
    }
    NSString *path = @"MobilePort/Index/Autoreply/set_autoreplay_word_s";
    NSDictionary *parame = @{[NSString stringWithFormat:@"%ld",(long)_MenuType]:@"replytype",view4.TF_3.text:@"word_content",view4.TF_1.text:@"funname",view4.TF_2.text:@"funkey"};
    [[HTTPGlobals shareHTTPGlobal] PostHTTPRequest:path parameters:parame finish:^(BOOL seccess, NSString *error, id obj) {
        if (seccess) {
            [self showText:@"设置成功"];
            DLog(@"请求成功:%@",obj);
            DLog(@"msg = %@",obj[@"msgreplyData"]);
            DLog(@"authinfo = %@",obj[@"pub_userinfo"][@"authinfo"]);
            DLog(@"info = %@",obj[@"pub_userinfo"][@"info"]);
            DLog(@"name = %@",obj[@"pub_userinfo"][@"name"]);
        }else {
            UIAlertView *alert = [self showDefaultAlertView];
            [alert show];
            DLog(@"请求失败");
        }
    }];
    
}
#pragma mark --图片发送按钮--
-(void)ImageBtnAction:(UIButton *)sender {
    [self showText:@"图片发送按钮"];
    if (!view2.TF_1.text.length || !view2.TF_2.text.length || !view2.TF_3.text.length){
        UIAlertView *alert = [self showAlertViewTitle:@"提示:" message:@"内容不能为空" Btntitle:@"确定"];
        [alert show];
        return;
    }
    if (![view2.ImageBtn backgroundImageForState:UIControlStateNormal]) {
        UIAlertView *alert = [self showAlertViewTitle:@"提示:" message:@"图片不能为空" Btntitle:@"确定"];
        [alert show];
        return;
    }
    NSString *path = @"MobilePort/Index/Autoreply/set_autoreplay_pic_s";
    NSDictionary *parame = @{[NSString stringWithFormat:@"%ld",(long)_MenuType]:@"replytype",view2.TF_3.text:@"word_content",view2.TF_1.text:@"funname",view2.TF_2.text:@"funkey"};
    UIImage *image = [view2.ImageBtn backgroundImageForState:UIControlStateNormal];
    [[HTTPGlobals shareHTTPGlobal] uploadImageWithUrl:path image:image imageName:@"pic_file" parameters:parame finish:^(BOOL seccess, NSError *error, id obj) {
        if (seccess) {
            [self showText:@"设置成功"];
            DLog(@"图片发送按钮 = %@",obj);
        }else {
            UIAlertView *alert = [self showDefaultAlertView];
            [alert show];
            DLog(@"error = %@",error);
        }
    }];
}
#pragma mark --单条新闻发送按钮--
-(void)OneNewsBtnAction:(UIButton *)sender {
    [self showText:@"单条新闻发送按钮"];
    if (!view3.TF_1.text.length || !view3.TF_2.text.length || !view3.textTitle.text.length || !view3.textIntroduction.text.length || !view3.textDetails.text.length) {
        UIAlertView *alert = [self showAlertViewTitle:@"提示:" message:@"内容不能为空" Btntitle:@"确定"];
        [alert show];
        return;
    }
    if (![view3.ImageBtn backgroundImageForState:UIControlStateNormal]) {
        UIAlertView *alert = [self showAlertViewTitle:@"提示:" message:@"图片不能为空" Btntitle:@"确定"];
        [alert show];
        return;
    }
    NSString *path = @"MobilePort/Index/Autoreply/set_autoreplay_news_s";
    NSDictionary *parame = @{};
    UIImage *image = [view3.ImageBtn backgroundImageForState:UIControlStateNormal];
    [[HTTPGlobals shareHTTPGlobal] uploadImageWithUrl:path image:image imageName:@"news_img" parameters:parame finish:^(BOOL seccess, NSError *error, id obj) {
        if (seccess) {
            [self showText:@"设置成功"];
            DLog(@"单条新闻发送按钮 = %@",obj);
        }else {
            UIAlertView *alert = [self showDefaultAlertView];
            [alert show];
            DLog(@"error = %@",error);
        }
    }];
}
#pragma mark --多条新闻发送按钮--
-(void)MoreNewsBtnAction:(UIButton *)sender {
    [self showText:@"多条新闻发送按钮"];
    NSMutableArray *textArr = [NSMutableArray array];
    for (int i = 0; i<10; i++) {
        UITextField *TF = (UITextField *)[self.view viewWithTag:3000+i];
        [textArr addObject:TF];
    }
    NSString *path = @"MobilePort/Index/Autoreply/set_autoreplay_mulnews_s";
    NSDictionary *parame = @{};
    NSMutableArray *imageArr = [NSMutableArray array];
    NSArray *nameArr = @[@"mulnews_img1",@"mulnews_img2",@"mulnews_img3",@"mulnews_img4"];
    for (int i = 0; i<4; i++) {
        UIButton *btn = (UIButton *)[self.view viewWithTag:3010+i];
        UIImage *image = [btn backgroundImageForState:UIControlStateNormal];
        [imageArr addObject:image];
    }
    [[HTTPGlobals shareHTTPGlobal] uploadImageWithUrl:path imageArr:imageArr imageNameArr:nameArr parameters:parame finish:^(BOOL seccess, NSError *error, id obj) {
        if (seccess) {
            [self showText:@"设置成功"];
            DLog(@"多张新闻发布按钮: %@",obj);
        }else {
            UIAlertView *alert = [self showDefaultAlertView];
            [alert show];
            DLog(@"error = %@",error);
        }
    }];
}
#pragma mark -
#pragma mark --CameraActionSheetDelegate--
- (void)cameraActionSheet:(CameraActionSheet *)sender didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 2) {
        return;
    }
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    if (buttonIndex == 0){
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    } else if (buttonIndex == 1) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        } else {
            //                [self showText:@"无法打开相机"];
        }
    }
    [self presentModalController:picker animated:YES];
}
#pragma mark --UIImagePickerControllerDelegate--
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:^() {
        UIImage * img = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        img = [UIImage rotateImage:img];
        VPImageCropperViewController *imgCropperVC = [[VPImageCropperViewController alloc] initWithImage:img cropFrame:CGRectMake((self.view.frame.size.width - 240)/2, 100.0f, 240, 400) limitScaleRatio:3.0 title:@"上传素材"];
        [imgCropperVC setCompletionBlock:^(BOOL didFinished, UIImage *editedImage) {
            if (editedImage) {
                [SelectBtn setBackgroundImage:editedImage forState:UIControlStateNormal];
                [SelectBtn setTitle:@"" forState:UIControlStateNormal];
            }
        }];
        [self pushViewController:imgCropperVC];
    }];
}


#pragma mark -
#pragma mark --tableViewDelegate--
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *indentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    
    return cell;
}
//#pragma mark --设置表头视图--
//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    UIView *headView = _headView;
//    return headView;
//}
//#pragma mark --设置表头的高度--
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    UIView *headView = [self tableView:tableView viewForHeaderInSection:section];
//    return CGRectGetHeight(headView.frame) + 100;
//}

#pragma mark -
#pragma mark --UIScrollViewDelegate--
// 当偏移量发生改变
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == _ScrollView) {
        offset = scrollView.contentOffset.x/320;
        if (_oldOffsetX == scrollView.contentOffset.x ) {
            return;
        }
        if (_oldOffsetY == scrollView.contentOffset.y && _oldOffsetX != scrollView.contentOffset.x && _oldOffsetY == 0) {
            int index = 0;
            for (int i = 0; i<secondaryBtnArr.count; i++) {
                UIButton *btn = secondaryBtnArr[i];
                btn.selected = NO;
                if (i == offset) {
                    index = i;
                }
            }
            [UIView animateWithDuration:0.2 animations:^{
                UIButton *btn = secondaryBtnArr[index];
                btn.selected = YES;
                secondaryLineView.frame = CGRectMake(CGRectGetMinX(btn.frame), CGRectHeadViewHeight*2,CGRectGetWidth(btn.frame), 2);
                
            } completion:^(BOOL finished) {
                
            }];
            if (index == 3) {
                _headView.frame = CGRectMake(0, 0, CGRectGetWidth(_tableView.frame), CGRectGetHeight(_tableView.frame)*2);
                [_tableView setTableHeaderView:_headView];
            }else {
                _headView.frame = CGRectMake(0, 0, CGRectGetWidth(_tableView.frame), CGRectGetHeight(_tableView.frame));
                [_tableView setTableHeaderView:_headView];
            }
            if (index == 2) {
                _headView.frame = CGRectMake(0, 0, CGRectGetWidth(_tableView.frame), CGRectGetHeight(_tableView.frame) + 40);
                [_tableView setTableHeaderView:_headView];
            }
            [_tableView beginUpdates];
            [_tableView setTableHeaderView:_headView];
            [_tableView endUpdates];
            
        }
        _oldOffsetY = scrollView.contentOffset.y;
        _oldOffsetX = scrollView.contentOffset.x;
        
        
    }
    
}
#pragma mark -
#pragma mark --TextFieldDelegate--
#pragma mark  --returen 回调--
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    
    CGRect frame = textField.frame;
    //在这里我多加了62，（加上了输入中文选择文字的view高度）这个依据自己需求而定
    int offset_y = (frame.origin.y+62 + 144)-(self.view.frame.size.height-256);//键盘高度216
    
    
    if (offset_y>0) {
        //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
        [UIView animateWithDuration:0.3 animations:^{
            _tableView.frame = CGRectMake(0.0f, -offset_y, self.view.frame.size.width, self.view.frame.size.height+offset_y);
        }];
        
    }
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
#pragma mark  -- 将要编辑完成时 --
- (void)textFieldDidEndEditing:(UITextField *)textField {
    [UIView animateWithDuration:0.3 animations:^{
        _tableView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
